/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.core.types;

import com.farmcode.api.runtime.FARMCodeContext;
import com.farmcode.api.types.DataTypesAPI;
import com.farmcode.api.types.Type;
import com.farmcode.api.types.TypeNotFoundException;
import com.farmcode.api.types.dao.ColumnByDatabaseDAO;
import com.farmcode.api.types.model.ColumnByDatabase;
import java.util.HashMap;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

/**
 *
 * @author oiprado
 */
@Local(DataTypesAPI.class)
@Stateless(name="ejb/dataTypes", mappedName="ejb/dataTypes")
public class DataTypesBean implements DataTypesAPI{

  @EJB
  private ColumnByDatabaseDAO columnByDatabaseDAO;
  private HashMap<Integer, Type> types = new HashMap<Integer, Type>();
  
  @Override
  public HashMap<Integer, Type> getTypes(FARMCodeContext context) throws TypeNotFoundException{
    String database = (String)context.get(FARMCodeContext.DATABASE);
    String language = (String)context.get(FARMCodeContext.LANGUAGE);
    List<ColumnByDatabase> result = columnByDatabaseDAO.findTypes(database, language);
    for (ColumnByDatabase columnByDatabase : result) {
      Type type = new Type();
      type.setCode(columnByDatabase.getColumnType().getCode());
      type.setDefaultValue(columnByDatabase.getColumnType().getDefaultValue());
      type.setName(columnByDatabase.getMapVal());
      types.put(type.getCode(), type);
    }
    return types;
  }
  
}
